<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      body {
        margin: 0;
        padding: 0;
        background: url(../images/grid.png);
      }
      canvas {
        background-color: rgba(255, 0, 0, 0.1);
      }
    </style>
  </head>
  <body>
    <canvas id="tutorial" width="300" height="300">
      您的浏览器不兼容canvas，请升级浏览器
    </canvas>

    <script>
      window.onload = function () {
        // 在浏览器下一次重绘会回调该函数的回调函数draw
        let canvasEl = document.querySelector('#tutorial')
        if (!canvasEl.getContext) return

        let ctx = canvasEl.getContext('2d')
        requestAnimationFrame(draw)

        function draw() {
          let second = new Date().getSeconds()
          ctx.clearRect(0, 0, 300, 300)
          ctx.save()
          ctx.translate(100, 100)
          ctx.rotate(((Math.PI * 2) / 60) * second)
          ctx.lineWidth = 6
          ctx.lineCap = 'round'
          ctx.strokeStyle = 'red'

          ctx.beginPath()
          ctx.moveTo(0, 0)
          ctx.lineTo(0, -80)
          ctx.stroke()

          ctx.restore()
          requestAnimationFrame(draw)
        }
      }
    </script>
  </body>
</html>
